clear all
use "ESS.dta" 

gen D=D_race
gen PD=PD_race
 label var D "Discriminatory attitudes"
 label var PD "Perceptions of discrimination"

 
 global y1 "D"
 global y2 "PD" 
 global x1 "total_eduyrs"
 global z1 "Age Age2 Female Edu_mum  i.Country  i.essround"
 global z2 " Female  Edu_mum i.Country  i.essround"
 global z1b "Age Age2 Female Edu_mum Africa Asia Muslim Jew i.Country  i.essround"
 global z2b " Female  Edu_mum i.Country Africa Asia Muslim Jew  i.essround"

 

local  CountryFE
local  TimeFE   
local  Sociodemographics
local  Estimator   


quietly: reg  $y1   $x1  
est store Correlation
estadd local CountryFE "No"
estadd local TimeFE "No"
estadd local  Sociodemographics "No"
estadd local  Estimator "OLS"  
 
quietly: reg  $y1   $x1  $z1  if Monotonic==1, robust
est store OLS
estadd local CountryFE "Yes"
estadd local TimeFE "Yes"
estadd local  Sociodemographics "Yes"
estadd local  Estimator "OLS"

 
drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"


quietly:rdbwselect  $y1  r if Monotonic==1,  covs(Edu_mum Female r1 r2 r3 r4 r5 r6 r7 r8 c1  c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 c15 c16 c17)
local u1 =floor(e(h_mserd))
g  w=max(0,`u1'-abs(r)) 

  ivreg2  $y1  ($x1 = T r)   $z2 [pweight= w] if Monotonic==1, first  savefirst savefprefix(fs_1) baselevels robust   gmm2s 
est store IV_all
mat l e(first)
local Fm= floor(e(first)[rownumb(e(first),"SWF"),1])
local N3=  e(N)
su  $y1 if e(sample), mean
loc Meanm:di%8.2fc r(mean)
drop w
estadd local CountryFE "Yes"
estadd local TimeFE "Yes"
estadd local  Sociodemographics "Yes"
estadd local  Estimator "GMM"



quietly: rdbwselect  $y1  r if Strong==1,  covs(Edu_mum Female  r1 r2 r3 r4 r5 r6 r7 r8 c1  c2 c3 c4 c5 c6 c7 c8 c9 c10   c10 c11 c12 c13 c14 c15 c16 c17)
local u2 =floor(e(h_mserd))
g w=max(0,`u2'-abs(r)) 

quietly: ivreg2  $y1  ($x1 = T r)   $z2  [pweight=w] if Strong==1, first  baselevels robust   gmm2s   savefirst savefprefix(fs_2)
est store IV_restricted
mat l e(first)
local Fs= floor(e(first)[rownumb(e(first),"SWF"),1])
local N4= e(N)
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Means=trim("`Mean'")
drop w
estadd local CountryFE "Yes"
estadd local TimeFE "Yes"
estadd local  Sociodemographics "Yes"
estadd local  Estimator "GMM"

esttab  Correlation OLS     IV_all  IV_restricted using "Tables/EthnoracialD.tex", replace se star(* 0.1 ** 0.05 *** 0.01) obslast collabels(, none)   wrap  legend  gaps nolz  booktabs mlabel("Corr" "OLS"  "IV (all)"  "IV (strong)")  nonumbers  cells(b(star fmt(3)  ) se(fmt(3) par  )) compress label title(Education and discriminatory ethnoracial attitudes, by specification.\label{EthnoracialD_table})   drop(Age Age2 Female Edu_mum   _cons *.Country  *.essround)      stats(  CountryFE  TimeFE Sociodemographics   Estimator N r2  widstat  , fmt(a1 a2 a3   a5 %9.0fc  2 %9.0fc  1  )  labels( `"Country FE"'  `"Time FE"' `"Sociodemographics"'   `"Estimator"' `"N.obs"' `"\(R^{2}\)"'  `"\(F\)-statistics"'   ) ) 
 
*****PERCEIVED DISCRIMINATION***********************************************************************

clear all
use "ESS.dta" 

drop total_eduyrs
gen total_eduyrs=  total_eduyrs2
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"


gen D=D_race
gen PD=PD_race
 label var D "Discriminatory attitudes"
 label var PD "Perceived discrimination"

 
 global y1 "D"
 global y2 "PD" 
 global x1 "total_eduyrs"
 global z1 "Age Female Edu_mum  i.Country  i.essround"
 global z2 "Female  Edu_mum i.Country  i.essround"
 global z1b "Age Age2 Female Edu_mum Africa Asia Muslim Jew i.Country  i.essround"
 global z2b "Female  Edu_mum i.Country Africa Asia Muslim Jew  i.essround"
 
 
la var total_eduyrs "Years of education"

local  CountryFE
local  TimeFE   
local  Sociodemographics
local  Estimator   

quietly: reg $y2  $x1  
est store Correlation
estadd local CountryFE "No"
estadd local TimeFE "No"
estadd local  Sociodemographics "No"
estadd local  Estimator "OLS"

quietly:  reg $y2 $x1 $z2b if  Monotonic==1, robust
est store OLS
estadd local CountryFE "Yes"
estadd local TimeFE "Yes"
estadd local  Sociodemographics "Yes"
estadd local  Estimator "OLS"


drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"

 
quietly:  rdbwselect  $y2  r if Monotonic==1,    covs(Edu_mum Female Africa Asia Muslim Jew r1 r2 r3 r4 r5 r6 r7 r8 c1  c2 c3 c4 c5 c6 c7 c8 c9 c10   c10 c11 c12 c13 c14 c15 c16 c17)
local u1 =floor(e(h_mserd))
g w=max(0,`u1'-abs(r)) 


quietly:  ivreg2 $y2  ($x1 = T r) $z2b  [pweight=w]  if Monotonic==1, first   savefirst savefprefix(fs_1) baselevels robust gmm2s 
est store IV_all
mat l e(first)
local Fm= floor(e(first)[rownumb(e(first),"SWF"),1])
local N3=  e(N)
su  $y2 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meanm=trim("`Mean'")
drop w
estadd local CountryFE "Yes"
estadd local TimeFE "Yes"
estadd local  Sociodemographics "Yes"
estadd local  Estimator "GMM"

quietly:  rdbwselect  $y2  r if Strong==1,    covs(Edu_mum Female Africa Asia Muslim Jew r1 r2 r3 r4 r5 r6 r7 r8 c1  c2 c3 c4 c5 c6 c7 c8 c9 c10   c10 c11 c12 c13 c14 c15 c16 c17)
local u2 =floor(e(h_mserd))
g w=max(0,`u2'-abs(r)) 

quietly: ivreg2 $y2  ($x1 = T r)  $z2b  [pweight=w]  if Strong==1, first   savefirst savefprefix(fs_2) baselevels robust   gmm2s  
est store IV_restricted
mat l e(first)
local Fs= floor(e(first)[rownumb(e(first),"SWF"),1])
local N4= e(N)
su  $y2 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Means=trim("`Mean'")
drop w
estadd local CountryFE "Yes"
estadd local TimeFE "Yes"
estadd local  Sociodemographics "Yes"
estadd local  Estimator "GMM"

 

esttab  Correlation OLS   IV_all  IV_restricted using "Tables/EthnoracialPD.tex", replace se star(* 0.1 ** 0.05 *** 0.01) obslast collabels(, none)   wrap  legend  gaps nolz  booktabs mlabel("Corr" "OLS" "IV (all)"  "IV (strong)")  nonumbers  cells(b(star fmt(3)  ) se(fmt(3) par  )) compress label title(Education and perceptions of ethnoracial discrimination, by specification.\label{EthnoracialPD_table})   drop(  Female Edu_mum Africa Asia Muslim Jew  _cons *.Country  *.essround)       stats(  CountryFE  TimeFE Sociodemographics   Estimator N r2  widstat  , fmt(a1 a2 a3   a5 %9.0fc  2 %9.0fc  1  )  labels( `"Country FE"'  `"Time FE"' `"Sociodemographics"'   `"Estimator"' `"N.obs"' `"\(R^{2}\)"'  `"\(F\)-statistics"'   ) ) 

